//
//  CompanyTaskCell.m
//  MarkPlatform
//
//  Created by xiao6 on 2017/12/27.
//  Copyright © 2017年 AT. All rights reserved.
//

#import "CompanyTaskCell.h"
#import "CompanyTaskPercentView.h"
#import "TaskRecordView.h"

@interface CompanyTaskCell()

@property (nonatomic, strong) UIImageView *bgImageView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *priceLabel;
@property (nonatomic, strong) UIView *detailLabelsView;
@property (nonatomic, strong) CompanyTaskPercentView *percentView;
@property (nonatomic, strong) UILabel *percentLabel;
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) UILabel *countLabel;
@property (nonatomic, strong) UIButton *button;
@end

@implementation CompanyTaskCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor clearColor];
        
        [self.contentView addSubview:self.bgImageView];
        [self.contentView addSubview:self.titleLabel];
        [self.contentView addSubview:self.priceLabel];
        [self.contentView addSubview:self.detailLabelsView];
        [self.contentView addSubview:self.percentView];
        [self.contentView addSubview:self.percentLabel];
        [self.contentView addSubview:self.timeLabel];
        [self.contentView addSubview:self.countLabel];
        [self.contentView addSubview:self.button];
        
        [self layoutViews];
    }
    return self;
}
- (void)layoutViews {
    [self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(6);
        make.bottom.offset(-6);
        make.leading.offset(10);
        make.trailing.offset(-10);
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgImageView).offset(20);
        make.left.equalTo(self.bgImageView).offset(20);
    }];
    [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.titleLabel);
        make.right.equalTo(self.bgImageView).offset(-15);
    }];
    [self.detailLabelsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel);
        make.right.equalTo(self.bgImageView).offset(-5);
        make.top.equalTo(self.titleLabel.mas_bottom).offset(5);
    }];
    [self.percentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.detailLabelsView.mas_bottom);
        make.height.equalTo(@20);
        make.left.equalTo(self.titleLabel);
        make.right.equalTo(self.percentLabel.mas_left).offset(-10);
    }];
    [self.percentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.percentView);
        make.right.equalTo(self.priceLabel);
    }];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel);
        make.top.equalTo(self.percentView.mas_bottom).offset(5);
    }];
    [self.countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel);
        make.top.equalTo(self.timeLabel.mas_bottom).offset(10);
        make.bottom.equalTo(self.bgImageView).offset(-20);
    }];
    [self.button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.bgImageView.mas_right).offset(-12);
        make.height.equalTo(@36);
        make.top.equalTo(self.percentView.mas_bottom).offset(9);
        make.width.equalTo(@100);
    }];
    [self setModel:nil];
}

- (void)setModel:(MPCompanyTaskModel *)model {
    _model = model;
    
    self.button.hidden = NO;
    switch (_model.status) {
        case MPCompanyTaskStatusDraf: {
            self.bgImageView.image = [UIImage imageNamed:@"card_renwuliebiao_caogao"];
            [self.button setTitle:@"删除" forState:UIControlStateNormal];
            [self.button setTarget:self action:@selector(deleteTask) forControlEvents:UIControlEventTouchUpInside];
            break;
        }
        case MPCompanyTaskStatusStart:
        case MPCompanyTaskStatusProgressing: {
            self.bgImageView.image = [UIImage imageNamed:@"card_renwuliebiao_jinxingzhong"];
            [self.button setTitle:@"暂停" forState:UIControlStateNormal];
            [self.button setTarget:self action:@selector(stopTask) forControlEvents:UIControlEventTouchUpInside];
            break;
        }
        case MPCompanyTaskStatusPause: {
            self.bgImageView.image = [UIImage imageNamed:@"card_renwuliebiao_yizanting"];
            [self.button setTitle:@"重启" forState:UIControlStateNormal];
            [self.button setTarget:self action:@selector(startTask) forControlEvents:UIControlEventTouchUpInside];
            break;
        }
        case MPCompanyTaskStatusFinish:
        default: {
            self.bgImageView.image = [UIImage imageNamed:@"card_renwuliebiao_yijiesu"];
            self.button.hidden = YES;
            break;
        }
    }
    
    self.titleLabel.text = _model.name;
    self.priceLabel.text = [NSString stringWithFormat:@"¥ %.2f",_model.price];;
    self.percentLabel.text = [NSString stringWithFormat:@"%@%%",@(_model.percent)];
    self.percentView.percent = _model.percent / 100.f;
    self.timeLabel.text = [@"截止时间:" stringByAppendingString: [[NSDate dateWithTimeIntervalSince1970:_model.endTime/1000.0f] stringWithFormat:@"yyyy-MM-dd HH:mm:ss"]];
    self.countLabel.text = [@"图片数量:" stringByAppendingFormat:@"%@",@(_model.quantity)];
    
    NSArray *arr = _model.step;
    [self.detailLabelsView removeAllSubviews];
    for (NSInteger i = 0; i < arr.count; i++) {
        TaskRecordView *view = [TaskRecordView viewWithNum:i+1 content:[arr[i] content]];
        [self.detailLabelsView addSubview:view];
        if (i == 0) {
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.offset(0);
                make.top.offset(5);
            }];
        }
        else {
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.offset(0);
                make.top.equalTo(self.detailLabelsView.subviews[i-1].mas_bottom).offset(3);
            }];
        }
        if (i == arr.count - 1) {
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.bottom.offset(-5);
            }];
        }
    }
}

#pragma mark - action
- (void)stopTask {
    [MPHttpTool GET:[NSString stringWithFormat:@"business/task/stopTask/%@", self.model.Id] params:nil success:^(NSURLSessionDataTask *task, ATApiResult *result) {
        self.model.status = MPCompanyTaskStatusPause;
        self.model = self.model;
    } failure:ATHttpFailure];
}
- (void)startTask {
    [MPHttpTool GET:[NSString stringWithFormat:@"business/task/startTask/%@", self.model.Id] params:nil success:^(NSURLSessionDataTask *task, ATApiResult *result) {
        self.model.status = MPCompanyTaskStatusStart;
        self.model = self.model;
    } failure:ATHttpFailure];
}
- (void)deleteTask {
    [MPHttpTool GET:[NSString stringWithFormat:@"business/task/deleteTask/%@", self.model.Id] params:nil success:^(NSURLSessionDataTask *task, ATApiResult *result) {
        self.model.status = MPCompanyTaskStatusFinish;
        self.model = self.model;
        !self.didDelete ?: self.didDelete(self);
    } failure:ATHttpFailure];
}


#pragma mark - getters
- (UIImageView *)bgImageView {
    if (!_bgImageView) {
        _bgImageView = [[UIImageView alloc] init];
    }
    return _bgImageView;
}
- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = [UIFont systemFontOfSize:17.f];
        _titleLabel.textColor = [UIColor colorWithRGB:0x333333];
    }
    return _titleLabel;
}
- (UILabel *)priceLabel {
    if (!_priceLabel) {
        _priceLabel = [[UILabel alloc] init];
        _priceLabel.font = [UIFont appHelveticaBold:17.f];;
        _priceLabel.textColor = [UIColor colorWithRGB:0xf8ba12];
    }
    return _priceLabel;
}
- (UIView *)detailLabelsView {
    if (!_detailLabelsView) {
        _detailLabelsView = [[UIView alloc] init];
    }
    return _detailLabelsView;
}
- (CompanyTaskPercentView *)percentView {
    if (!_percentView) {
        _percentView = [[CompanyTaskPercentView alloc] init];
    }
    return _percentView;
}
- (UILabel *)percentLabel {
    if (!_percentLabel) {
        _percentLabel = [[UILabel alloc] init];
        _percentLabel.font = [UIFont fontWithName:@"Helvetica" size:13.f];
        _percentLabel.textColor = [UIColor colorWithRGB:0x666666];
    }
    return _percentLabel;
}
- (UILabel *)timeLabel {
    if (!_timeLabel) {
        _timeLabel = [[UILabel alloc] init];
        _timeLabel.font = [UIFont systemFontOfSize:13.f];
        _timeLabel.textColor = [UIColor colorWithRGB:0x999999];
    }
    return _timeLabel;
}
- (UILabel *)countLabel {
    if (!_countLabel) {
        _countLabel = [[UILabel alloc] init];
        _countLabel.font = [UIFont systemFontOfSize:13.f];
        _countLabel.textColor = [UIColor colorWithRGB:0x999999];
    }
    return _countLabel;
}
- (UIButton *)button {
    if (!_button) {
        _button = [MPAppButton buttonWithType:UIButtonTypeCustom];
    }
    return _button;
}

@end
